//
//  SHD_ChipViewController.m
//  B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_ChipViewController.h"
#import "XMN_PostsViewController.h"
@interface SHD_ChipViewController ()
@property(nonatomic, strong)XMN_ProgressHUD *HUD;
@end

@implementation SHD_ChipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"碎片";
    
    _HUD = [[XMN_ProgressHUD alloc]initWithFrame:self.view.frame];
    _HUD.backgroundColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
    _HUD.XMN_label.text = @"Waiting...";
    [self.view addSubview:_HUD];
}
-(void)jumpVC
{
    SHD_TagViewController *vc = [[SHD_TagViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    _chipMainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375 *FitWidth, (667 - 64) * FitHeight )];
    _chipMainTableView.dataSource= self;
    _chipMainTableView.delegate = self;
    [_chipMainTableView NightWithType:UIViewColorType2];
    [self.view addSubview:_chipMainTableView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"XMN_caidan"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpVC)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
   _chipMainTableView.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
      [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
     [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _chipMainTableView.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_chipMainTableView.header beginRefreshing];
   
}
-(void)getData
{
    if ([_chipMainTableView.header isRefreshing]) {
        _flag = 0;
    }
    else
    {
        _flag += 10;
    }


    [HTTPMethod getDataByString:@"http://api2.pianke.me/timeline/list" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&limit=10&start=%ld&version=3.0.6", _flag ]] WithDataBlock:^(id data) {
        if (data != nil && _flag == 0) {
            [_dataArray removeAllObjects];
        }
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            SHD_ChipMainModel *chip = [[SHD_ChipMainModel alloc]init];
            [chip setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:chip];
        }
        [_chipMainTableView reloadData];
        [_chipMainTableView.header endRefreshing];
        [_chipMainTableView.footer endRefreshing];
        [_HUD removeFromSuperview];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SHD_ChipDataTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell ) {
        cell = [[SHD_ChipDataTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    static NSString *noImageIden = @"noImageCell";
    SHD_NOImageCell *noImageCell = [tableView dequeueReusableCellWithIdentifier:noImageIden];
    if (!noImageCell ) {
        noImageCell = [[SHD_NOImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:noImageIden];
    }

    if (![[_dataArray[indexPath.row] coverimg] isEqualToString:@""])
    {
        
        cell.chipModel = _dataArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nav = self.navigationController;
        return cell;
    }
    else
    {
        noImageCell.chipModel = _dataArray[indexPath.row];
        noImageCell.selectionStyle = UITableViewCellSelectionStyleNone;
        noImageCell.nav = self.navigationController;
        return noImageCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%@, %ld", [_dataArray[indexPath.row] coverimg],  indexPath.row);
    if (![[_dataArray[indexPath.row] coverimg] isEqualToString:@""]) {
        NSArray *whArray = [[_dataArray[indexPath.row] coverimg_wh] componentsSeparatedByString:@"*"];
        CGFloat hight = [whArray[1] floatValue] * 380 * FitHeight / [whArray[0] floatValue];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, nil];
       CGRect rect = [[_dataArray[indexPath.row] content] boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
      
        return hight + ceil(rect.size.height) + 130 * FitHeight;
    }
    else
    {
      
       
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18], NSFontAttributeName, nil];
        CGRect rect = [[_dataArray[indexPath.row] content] boundingRectWithSize:CGSizeMake(345 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return  ceil(rect.size.height) + 100 * FitHeight;

        
        
    }
}
#pragma mark 点击进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMN_PostsViewController *cwv = [[XMN_PostsViewController alloc]init];
    [self.navigationController pushViewController:cwv animated:YES];
    cwv.idString = [_dataArray[indexPath.row] contentid];
    cwv.contentString = [_dataArray[indexPath.row] content];
    //cwv.titleString = [_dataArray[indexPath.row] title];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
